package com.ruoyi.vim.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.DefaultStatus;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.vim.mapper.ImWelcomeWordMapper;
import com.ruoyi.vim.domain.ImWelcomeWord;
import com.ruoyi.vim.service.IImWelcomeWordService;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * 欢迎语配置Service业务层处理
 *
 * @author wgp
 * @since 2022-12-10
 */
@Service
public class ImWelcomeWordServiceImpl extends ServiceImpl<ImWelcomeWordMapper, ImWelcomeWord> implements IImWelcomeWordService {

    @Override
    public boolean add(ImWelcomeWord imWelcomeWord) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        imWelcomeWord.setCreateBy(String.valueOf(user.getUserId()));
        imWelcomeWord.setCreateTime(new Date());
        return this.save(imWelcomeWord);
    }

    @Override
    public boolean edit(ImWelcomeWord imWelcomeWord) {
        if(imWelcomeWord.getId() != null) {
            ImWelcomeWord welcomeWord = this.getById(imWelcomeWord.getId());
            BeanUtils.copyProperties(imWelcomeWord, welcomeWord);
            SysUser user = SecurityUtils.getLoginUser().getUser();
            welcomeWord.setUpdateBy(String.valueOf(user.getUserId()));
            welcomeWord.setUpdateTime(new Date());
            return this.updateById(welcomeWord);
        }
        return false;
    }

    @Override
    public boolean setDefault(ImWelcomeWord imWelcomeWord) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        //把原来的默认设置非默认
        UpdateWrapper<ImWelcomeWord> warp = new UpdateWrapper<>();
        warp.set("is_default",DefaultStatus.NO.getCode())
                .set("update_by",String.valueOf(user.getUserId()))
                .set("update_time",new Date())
                .eq("is_default", DefaultStatus.YES.getCode());
        this.update(warp);

        warp.clear();
        warp.set("is_default",DefaultStatus.YES.getCode())
                .set("update_by",String.valueOf(user.getUserId()))
                .set("update_time",new Date())
                .eq("id", imWelcomeWord.getId());
        return this.update(warp);
    }
}
